package com.app.twth.ui.tally.lpn;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.checkin.FileUpload;
import com.app.twth.ui.tally.sku.TallyQr;
import com.app.twth.utils.DateUtil;
import com.lzy.okgo.model.Response;

import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class TallyLPNViewModel extends BaseAndroidViewModel<TallyLPNDataModel> {

    public MutableLiveData<List<TalliedSku>> skuListMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<List<TalliedSize>> sizeListMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<FileUpload> fileUploadMutableLiveData = new MutableLiveData<>();

    public TallyLPNViewModel(@NonNull Application application) {
        super(application);
    }

    public void getIbLpnSkuList(String lpn) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.getIbLpnSkuList(lpn, new JsonCallBack<BaseResponseEntity<List<TalliedSku>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<TalliedSku>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<TalliedSku>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    skuListMutableLiveData.setValue(baseResponseEntity.data);
                    getIbLpnSize(lpn);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }
    public void getIbLpnSize(String lpn) {
        isLoading.setValue(new LoadingBean(false, Constant.loading));
        errorResultCode.setValue("");
        mModel.gettallyPalletUnit(lpn, new JsonCallBack<BaseResponseEntity<List<TalliedSize>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<TalliedSize>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<TalliedSize>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    sizeListMutableLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void fileUpload(String containerNo, String fileName, String path, int position, boolean isSubmit, boolean showDialog) {
        isLoading.setValue(new LoadingBean(showDialog, Constant.loading));
        String name = "lpnphoto/" + containerNo + "-" + fileName + "-" + DateUtil.getYMD() + "-" + System.currentTimeMillis() + ".jpg";
        mModel.upload(name, path, new JsonCallBack<BaseResponseEntity<String>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<String>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<String> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FileUpload fileUpload = new FileUpload();
                    fileUpload.setPosition(position);
                    fileUpload.setPath(baseResponseEntity.data);
                    fileUpload.setSubmit(isSubmit);
                    fileUploadMutableLiveData.setValue(fileUpload);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(baseResponseEntity.getCode());
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(error);
            }
        });
    }


    public void tallySubmit(TallyQr tallyQr) {
        isLoading.setValue(new LoadingBean(true, Constant.load_submission));
        errorResultCode.setValue("");
        ((TallyLPNDataModel) mModel).tallySubmit(tallyQr, new JsonCallBack<BaseResponseEntity<Object>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<Object>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<Object> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    infoResultCode.setValue(baseResponseEntity.getCode());
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }


}
